#include "cmdline.h"

#include <string.h>
#include "utils/error.h"

#define COMMAND_POSITION 1
#define DIRECTORY_POSITION 2

struct options_mappings
{
  const char *long_opt;
  ARGUMENT_TYPE argument;
};

static struct options_mappings options_mappings[] =
{
  { "languages", LANGUAGES },
  { "news", NEWS },
  { "categories", CATEGORIES },
  { "threads", THREADS },
  { "top", TOP }
};

static const size_t options_mappings_length = sizeof (options_mappings) / sizeof (*options_mappings);

static ARGUMENT_TYPE
map_argument(const char *argument)
{
  struct options_mappings *mapping = options_mappings;
  do
    {
      if (strcmp(argument, mapping->long_opt) == 0)
        {
          return mapping->argument;
        }
    }
  while (++mapping != options_mappings + options_mappings_length);

  PRINT_ERROR_EXIT("Unknown command argument");
}

tgnews_options_t
parse_cmdline(int argc, char **argv)
{
  if (argc != 3)
    {
      PRINT_ERROR_EXIT("Too few arguments");
    }

  tgnews_options_t options;
  options.argument = map_argument(argv[COMMAND_POSITION]);
  options.directory = argv[DIRECTORY_POSITION];

  return options;
}
